package com.uuah.server.config.spring.parsers.delegate;

import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

import com.uuah.server.config.spring.parsers.IUuahDefinitionParser;

public class BooleanAttributeSelectionDefinitionParser extends
		AbstractParallelDelegatingDefinitionParser {

	private String attribute;
	private boolean dflt;
	private IUuahDefinitionParser whenTrue;
	private IUuahDefinitionParser whenFalse;

	public BooleanAttributeSelectionDefinitionParser(String attribute,
			boolean dflt, IUuahDefinitionParser whenTrue,
			IUuahDefinitionParser whenFalse) {
		super(new IUuahDefinitionParser[] { whenTrue, whenFalse });
		this.attribute = attribute;
		this.dflt = dflt;
		this.whenTrue = whenTrue;
		this.whenFalse = whenFalse;
		addIgnored(attribute);
	}

	protected IUuahDefinitionParser getDelegate(Element element,
			ParserContext parserContext) {
		boolean value = dflt;
		if (null != element && element.hasAttribute(attribute)) {
			value = Boolean.valueOf(element.getAttribute(attribute))
					.booleanValue();
		}
		if (value) {
			return whenTrue;
		} else {
			return whenFalse;
		}
	}

}
